<template>
	<view class="base-tab">
		<view
			v-for="(item, idx) in props.tabs"
			:class="`base-tab__item ${activeIndex == idx ? 'active' : ''}`"
			:key="idx"
			@click="handleSwitchTab(idx)"
		>
			{{ item[props.defaultProps.label] }}
		</view>
	</view>
</template>

<script setup>
const emits = defineEmits(['update:active', 'change']);

const props = defineProps({
	tabs: { type: Array, default: () => [] },
	active: { type: [Number, String], default: 0 },
	defaultProps: {
		type: Object,
		default: () => ({
			label: 'label',
			name: 'name'
		})
	}
});

const activeIndex = ref(props.active);

const handleSwitchTab = idx => activeIndex.value = idx;

watch(
	() => props.active,
	val => {
		activeIndex.value = val;
	}
);

watch(
	() => activeIndex.value,
	val => {
		emits('update:active', val);
    emits('change', props.tabs[val])
	}
);
</script>

<style lang="scss">
.base-tab {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px;
  background: #fff;
  border-bottom: 1px solid #f5f5f5;

  &__item {
    flex: 1;
    text-align: center;
    line-height: 48px;
    color: #999;
    font-size: 14px;

    &.active {
      color: #4c7bfd;
      font-weight: bold;
      position: relative;

      &::after {
        content: '';
        position: absolute;
        left: 0;
        bottom: 0;
        right: 0;
        width: 60%;
        height: 2px;
        border-radius: 2px;
        background: #4c7bfd;
        margin: 0 auto;
      }
    }
  }
}
</style>
